package com.dss.auto_deploy.rabbitmq.listener;

import com.alibaba.fastjson.JSONObject;
import com.dss.auto_deploy.dto.GitlabPushDto;
import com.dss.auto_deploy.service.IAdmDockerDeployService;
import com.dss.auto_deploy.service.IGitlabPushService;
import com.dss.auto_deploy.util.JacksonUtils;
import com.dss.auto_deploy.vo.MqMsgVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import static com.dss.auto_deploy.constants.GitlabConstants.GITLAB_QUEUE_NAME;

/**
 * @Description:
 * @author: dongshoushan
 * @date: 2022年11月04日 13:58
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class GitlabListener {
    private final IGitlabPushService gitlabPushService;
    private final IAdmDockerDeployService admDockerDeployService;
    private final ApplicationEventPublisher publisher;
    @RabbitHandler
    @RabbitListener(queues = GITLAB_QUEUE_NAME)
    public void message(String string) {
        MqMsgVo<GitlabPushDto> msgVo = JacksonUtils.toBean(string, MqMsgVo.class);
        String s = JSONObject.toJSONString(msgVo.getData());
        GitlabPushDto gitlabPushDto = JacksonUtils.toBean(s, GitlabPushDto.class);
        gitlabPushService.toDeployDocker(gitlabPushDto);
    }
    //@RabbitHandler
    //@RabbitListener(queues = DEPLOY_PULL_QUEUE)
    //public void deployPull(String string) {
    //    MqMsgVo<DockerDeployMqDto> msgVo = JacksonUtils.toBean(string, MqMsgVo.class);
    //    String s = JSONObject.toJSONString(msgVo.getData());
    //    DockerDeployMqDto dto = JacksonUtils.toBean(s, DockerDeployMqDto.class);
    //    try {
    //        AdmDockerDeploy admDockerDeploy = dto.getAdmDockerDeploy();
    //        String imageName = dto.getImageName();
    //        publisher.publishEvent(new DockerPullAndStartEvent(admDockerDeploy, imageName));
    //    }catch (Exception e){
    //        e.printStackTrace();
    //    }
    //}
}
